// pages/minePage/addNewDevice/addNewDevice.js
import {
  lgRequest
} from '../../../utils/ajax'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    titleBarHeight: getApp().globalData.titleBarHeight,
    sysInfo: getApp().globalData.sysInfo,
    isIphoneX: getApp().globalData.isIphoneX,
    addLeftImg: 'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/176ceec17a54774670e59815ed39d8bb.png',
    doenLoadImg: 'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/65136abbea1670346fc9582f797b3416.png',
    showSelect: false,
    delectImg: 'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/713a49f43240fae4d44090d92d7f9cf6.png',
    addSupplierPop: false,
    addDeviceInfo: {

    },
    addSuoolier: {
      supplierName: '',
      contactName: '',
      contactPhone: ''
    },
    EquipmentL: '',
    EquipmentN: '',
    EquipmentX: '',
    deviceInfoList: [], //设备列表
    showSelectDevice: false,
    addDevicePop: false,
    selectItemSupplier: '',
    isDevc: false,
    showBatch: false,
    purchaseDate: '',
    batchTimeData: '',
    isShowming: false,
    isShowsao: false,
    isShowmingxi: false,
    isShowS: false, // 控制设备类别的显隐
    Shebeileibie: [],
    shebeiM:'',//输入编号查找设备
    Sbianma:'',//扫描或手动输入设备编号
  },
    // 通过code获取字典对应值
    getCode() {
      lgRequest('getCode', {}, {
        doSuccess: r => {
          console.log(r.data, 'getCode查询值')
          this.setData({
            Shebeileibie: r.data
          })
        }
      })
    },
  // 查看扫码录入
  chakansao() {
    this.setData({
      isShowsao: true
    })
  },
  // 关闭扫码录入
  closesao() {
    this.setData({
      isShowsao: false
    })
  },
  // 查看明细
  chakanming() {
    // console.log(111);
    this.setData({
      isShowming: true
    })
  },
  // 搜索设备明细
  closeming() {
    let params = {}
      lgRequest('shebeiM', params, {
        errAlert: false,
        doSuccess: r => {
          if (r.code == 200) {
            console.log(r);
            // this.setData({
            //   supplierList: r.data
            // })
          } else {
            wx.showToast({
              title: r.msg,
              icon: 'none'
            })
          }
        },
        doComplete: r => {}
      })
  },
  //获取供应商列表
  supplierList() {
    let params = {}
    lgRequest('supplierList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          this.setData({
            supplierList: r.data
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {}
    })
  },
  //获取设备列表
  deviceList() {
    let params = {}
    lgRequest('deviceList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          console.log(r.data,'@@@');
          this.setData({
            deviceInfoList: r.data
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {}
    })
  },
  //选择供应商
  selectLine(e) {
    console.log(e);
    let type = e.currentTarget.dataset.type
    let selectItem = e.currentTarget.dataset.item

    switch (type) {
      case '0':
        this.setData({
          showSelect: false,
          selectItemSupplier: selectItem,
          isDevc: true
        })
        break;
      case '1':
        this.setData({
          showSelectDevice: false,
        })
        if (this.data.isDevc) {
          this.setData({
            selectItemDevice: selectItem
          })
        } else {
          wx.showToast({
            title: '请选择供应商！',
            icon: 'none'
          })
        }
        break;
    }

  },
  //重置
  resertBtn() {
    this.setData({
      addSuoolier: Object.assign({}, this.data.addSuoolier)
    })
  },
  // 展示日历
  showRiLi(e) {
    console.log(e,'这是日期');
    this.setData({
      showRiLi: true
    })
  },
  // 关闭弹窗
  onClose() {
    this.setData({
      showRiLi: false
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getYear()+1900}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  //保修期
  Cbaoxiu(e){
    console.log(e.detail.value);
    this.setData({
      batchTimeData:e.detail.value
    })
  },
  //日历
  getPicker(event) {
    let data = this.formatDate(event.detail);
    this.setData({
      purchaseDate: data,
      showRiLi: false
    })
    // console.log(event.detail)
    // console.log(data, '@@@@')
  },
  onInput(event) {
    console.log(event)
    this.setData({
      currentDate: event.detail,
    });
  },
  //打开设备选择
  // adaddEquipment
  chooseS() {
    // console.log(111);
    this.setData({
      isShowS: true,
      addDevicePop: false,
    })
  },
  //关闭设备类别
  closeS() {
    this.setData({
      isShowS: false,
      addDevicePop: true,
    })
  },
  //添加设备
  adaddEquipment() {
    let params = {
      deviceName: this.data.EquipmentN,
      deviceModel: this.data.EquipmentX,
      deviceType: this.data.EquipmentI,
    }
    // console.log(params);
    lgRequest('addEquipment', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          // console.log(r);
          this.setData({
            EquipmentN: "",
            EquipmentX: "",
            EquipmentL: "",
            addDevicePop: !this.data.addDevicePop
          })
          // this.resertBtn()
          this.deviceList()
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {}
    })
  },
  //选择设备类别
  chooseSB(e) {
    // console.log(e.currentTarget.dataset.sb);
    this.setData({
      EquipmentL: e.currentTarget.dataset.sb.valueName,
      EquipmentI:e.currentTarget.dataset.sb.valueId,
      isShowS: false,
      addDevicePop: true,
    })
  },
  //添加供应商
  addSupplier() {

    if (!this.data.addSuoolier.supplierName || !this.data.addSuoolier.contactName || !this.data.addSuoolier.contactPhone) {
      wx.showToast({
        title: '请输入必填项！',
        icon: 'none'
      })
      return
    }
    lgRequest('addSupplier', this.data.addSuoolier, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          this.setData({
            showSelect: false
          })
          this.resertBtn()
          this.supplierList()
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {}
    })
    this.setData({
      addSupplierPop: false,
      addSuoolier: {
        supplierName: '',
        contactName: '',
        contactPhone: ''
      },
    })
  },
  bindKeyWord(e) {
    // console.log(111);
    // console.log(e.detail.value);
    this.setData({
      EquipmentN: e.detail.value,
    })
    // let name = e.currentTarget.dataset.name
    // this.setData({
    //   [name] :e.detail.value
    // })
  },
  bindKeyWord_1(e) {
    this.setData({
      EquipmentX: e.detail.value
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 控制添加批次的关闭
  showSelectPop1() {
    let params = {
      project:'1',
      purchaseTime:this.data.purchaseDate * 1,
    }
    // console.log(this.data.purchaseDate);
    lgRequest('addBatch', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          console.log(r);
        } else {
          console.log(r);
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
        // console.log(r);
      }
    })
    this.setData({
      showBatch: !this.data.showBatch,
      isShowmingxi: true,
      // purchaseDate: '',
      // batchTimeData: '',
    })
  },
  showSelectPop(e) {
    let type = e.currentTarget.dataset.type
    switch (type) {
      case '0':
        this.setData({
          addSupplierPop: !this.data.addSupplierPop
        })
        break;
      case '1':
        this.setData({
          showSelect: !this.data.showSelect
        })
        break;
      case '2':
        this.setData({
          addDevicePop: !this.data.addDevicePop
        })
        break;
      case '3':
        this.setData({
          showSelectDevice: !this.data.showSelectDevice
        })
        break;
      case '4':
        this.setData({
          showBatch: !this.data.showBatch,
        })
        break;
      case '5':
        this.setData({
          isShowming: !this.data.isShowming,
        })
        break;
      case '6':
        this.setData({
          isShowsao: !this.data.isShowsao,
        })
        break;


    }

  },
  //扫码功能
  scanCode(){
    // console.log(111);
    var that = this
    wx.scanCode({
      success(res) {
        // console.log(res)
        that.setData({
          Sbianma:res.result
        })
      }
    })
    // console.log(222);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.supplierList()
    this.deviceList()
    this.getCode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})